<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>在线五子棋注册</title>
    <link rel="stylesheet" href="./css/common.css">
    <link rel="stylesheet" href="./css/login.css">
    <style>
        /* 基础样式 */
        * {
            box-sizing: border-box;
        }
        
        body {
            margin: 0;
            padding: 0;
            font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
        }
        
        .nav {
            height: 50px;
            background-color: rgb(50, 50, 50);
            color: white;
            line-height: 50px;
            padding-left: 20px;
            font-size: 18px;
        }
        
        .login-container {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: calc(100vh - 50px);
            padding: 20px;
        }
        
        .login-dialog {
            width: 100%;
            max-width: 450px;
            background-color: rgba(255, 255, 255, 0.92);
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
            padding: 25px;
            animation: fadeInUp 0.6s ease-out;
        }
        
        .login-dialog h3 {
            text-align: center;
            padding: 10px 0 25px;
            font-size: 28px;
            color: #333;
            margin: 0;
        }
        
        /* 输入框容器 */
        .input-container {
            position: relative;
            margin: 20px 0;
            width: 100%;
        }
        
        .input-container input {
            width: 100%;
            padding: 15px;
            border: 2px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
            background-color: rgba(255, 255, 255, 0.9);
            transition: all 0.3s ease;
        }
        
        .input-container input:focus {
            border-color: #2196F3;
            box-shadow: 0 0 8px rgba(33, 150, 243, 0.3);
            outline: none;
        }
        
        .input-container label {
            position: absolute;
            top: 15px;
            left: 15px;
            color: #777;
            pointer-events: none;
            transition: all 0.3s ease;
            background-color: transparent;
            padding: 0 5px;
        }
        
        .input-container input:focus+label,
        .input-container input:not(:placeholder-shown)+label {
            top: -10px;
            left: 10px;
            font-size: 14px;
            color: #2196F3;
            background-color: rgba(255, 255, 255, 0.9);
        }
        
        /* 按钮样式 */
        #submit {
            transition: all 0.3s ease;
            transform: translateY(0);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            background-color: #2196F3;
            width: 100%;
            height: 50px;
            color: white;
            border: none;
            outline: none;
            border-radius: 8px;
            margin-top: 10px;
            font-size: 18px;
            font-weight: 600;
            cursor: pointer;
        }
        
        #submit:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
            background-color: #1976D2;
        }
        
        #submit:active {
            transform: translateY(1px);
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        
        /* 链接样式 */
        .link-container {
            margin-top: 15px;
            text-align: center;
        }
        
        .link-container a {
            color: #2196F3;
            text-decoration: none;
            font-weight: 600;
            transition: all 0.2s ease;
        }
        
        .link-container a:hover {
            color: #1976D2;
            text-decoration: underline;
        }
        
        /* 错误信息样式 */
        .error-message {
            display: none;
            background-color: #ffebee;
            color: #f44336;
            padding: 12px;
            border-radius: 8px;
            margin: 10px 0;
            text-align: center;
            animation: fadeIn 0.3s ease-out;
        }
        
        .success-message {
            display: none;
            background-color: #e8f5e9;
            color: #4CAF50;
            padding: 12px;
            border-radius: 8px;
            margin: 10px 0;
            text-align: center;
            animation: fadeIn 0.3s ease-out;
        }
        
        /* 字段错误提示 */
        .field-error {
            color: #f44336;
            font-size: 12px;
            margin-top: 5px;
            display: block;
            text-align: left;
            padding-left: 5px;
            animation: fadeIn 0.3s ease-out;
        }
        
        .input-error {
            border-color: #f44336 !important;
        }
        
        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        /* 响应式设计 */
        @media (max-width: 480px) {
            .login-container {
                padding: 15px;
            }
            
            .login-dialog {
                padding: 20px;
            }
            
            .login-dialog h3 {
                font-size: 24px;
                padding: 5px 0 20px;
            }
            
            .input-container {
                margin: 15px 0;
            }
            
            .input-container input {
                padding: 12px;
                font-size: 15px;
            }
            
            .input-container label {
                font-size: 14px;
            }
            
            #submit {
                height: 45px;
                font-size: 16px;
            }
            
            .link-container {
                font-size: 14px;
            }
        }
        
        @media (max-width: 360px) {
            .login-dialog {
                padding: 15px;
            }
            
            .login-dialog h3 {
                font-size: 22px;
            }
            
            .input-container input {
                padding: 10px;
                font-size: 14px;
            }
            
            .input-container label {
                font-size: 13px;
            }
            
            #submit {
                height: 42px;
                font-size: 15px;
            }
        }
    </style>
</head>

<body>
    <div class="nav">
        在线五子棋注册页面
    </div>
    <div class="login-container">
        <div class="login-dialog">
            <h3>注册</h3>

            <!-- 错误信息展示区域 -->
            <div id="error-message" class="error-message"></div>
            <div id="success-message" class="success-message"></div>

            <div class="input-container">
                <input type="text" id="user_name" placeholder=" " oninput="validateField(this)">
                <label for="user_name">用户名</label>
                <span id="username-error" class="field-error"></span>
            </div>

            <div class="input-container">
                <input type="password" id="password" placeholder=" " oninput="validateField(this)">
                <label for="password">密码</label>
                <span id="password-error" class="field-error"></span>
            </div>

            <div class="input-container">
                <input type="password" id="confirm_password" placeholder=" " oninput="validatePasswordMatch()">
                <label for="confirm_password">确认密码</label>
                <span id="confirm-password-error" class="field-error"></span>
            </div>

            <button id="submit">注册</button>

            <div class="link-container">
                <a href="/login.html">已有账号？点击登录</a>
            </div>
        </div>
    </div>

    <script src="./js/jquery.min.js"></script>
    <script>
        // 确保注册按钮绑定事件
        document.getElementById('submit').addEventListener('click', reg);

        // 前端验证功能
        function validateField(field) {
            const value = field.value.trim();
            const errorElement = document.getElementById(`${field.id}-error`);

            // 清除错误状态
            field.classList.remove('input-error');
            if (errorElement) errorElement.textContent = '';

            // 非空验证
            if (!value) {
                field.classList.add('input-error');
                if (errorElement) errorElement.textContent = '此项为必填项';
                return false;
            }

            // 用户名长度验证
            if (field.id === 'user_name' && value.length < 3) {
                field.classList.add('input-error');
                if (errorElement) errorElement.textContent = '用户名至少需要3个字符';
                return false;
            }

            // 密码长度验证
            if (field.id === 'password' && value.length < 6) {
                field.classList.add('input-error');
                if (errorElement) errorElement.textContent = '密码至少需要6个字符';
                return false;
            }

            return true;
        }

        // 密码一致性验证
        function validatePasswordMatch() {
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirm_password').value;
            const errorElement = document.getElementById('confirm-password-error');
            const confirmField = document.getElementById('confirm_password');

            // 清除错误状态
            if (confirmField) confirmField.classList.remove('input-error');
            if (errorElement) errorElement.textContent = '';

            if (password && confirmPassword && password !== confirmPassword) {
                confirmField.classList.add('input-error');
                errorElement.textContent = '两次输入的密码不一致';
                return false;
            }

            return true;
        }

        // 表单整体验证
        function validateForm() {
            let isValid = true;

            // 验证用户名
            const usernameField = document.getElementById('user_name');
            if (!validateField(usernameField)) isValid = false;

            // 验证密码
            const passwordField = document.getElementById('password');
            if (!validateField(passwordField)) isValid = false;

            // 验证密码一致性
            if (!validatePasswordMatch()) isValid = false;

            return isValid;
        }

        // 注册函数（确保包含原始后端交互逻辑）
        function reg() {
            // 清空错误信息
            document.getElementById('error-message').style.display = 'none';
            document.getElementById('success-message').style.display = 'none';

            // 执行表单验证
            if (!validateForm()) {
                // 显示总体错误提示
                const errorElement = document.getElementById('error-message');
                errorElement.textContent = '请完善表单信息后再提交';
                errorElement.style.display = 'block';
                return;
            }

            // 获取表单数据（与原始代码一致）
            var reg_info = {
                username: document.getElementById("user_name").value,
                password: document.getElementById("password").value
            };

            // 原始ajax调用代码保持不变
            $.ajax({
                url: "/reg",
                type: "post",
                data: JSON.stringify(reg_info),
                success: function (res) {
                    if (res.result == false) {
                        // 显示错误信息而不是alert
                        const errorElement = document.getElementById('error-message');
                        errorElement.textContent = res.reason || "注册失败，请重试";
                        errorElement.style.display = 'block';

                        // 清空用户名和密码（与原始代码一致）
                        document.getElementById("user_name").value = "";
                        document.getElementById("password").value = "";
                        if (document.getElementById("confirm_password")) {
                            document.getElementById("confirm_password").value = "";
                        }
                    } else {
                        // 显示成功信息而不是alert
                        const successElement = document.getElementById('success-message');
                        successElement.textContent = res.reason || "注册成功！即将跳转...";
                        successElement.style.display = 'block';

                        // 1.5秒后自动跳转到登录页面（原始代码直接跳转）
                        setTimeout(() => {
                            window.location.assign("/login.html");
                        }, 1500);
                    }
                },
                error: function (xhr) {
                    // 从响应中提取错误信息
                    let errorMsg = "注册失败，请重试";
                    if (xhr.responseJSON && xhr.responseJSON.reason) {
                        errorMsg = xhr.responseJSON.reason;
                    } else if (xhr.status === 400) {
                        errorMsg = "注册信息有误";
                    }

                    // 显示错误信息而不是alert
                    const errorElement = document.getElementById('error-message');
                    errorElement.textContent = errorMsg;
                    errorElement.style.display = 'block';

                    // 清空用户名和密码（与原始代码一致）
                    document.getElementById("user_name").value = "";
                    document.getElementById("password").value = "";
                    if (document.getElementById("confirm_password")) {
                        document.getElementById("confirm_password").value = "";
                    }
                }
            });
        }
    </script>
</body>

</html>